﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Dimok.Web.UI;
using Dimok.Aspx.Manager;
using Data.LINQ;

public partial class Controls_USIX_Bridge_CollectionInfo : System.Web.UI.UserControl,IScriptControl {
	UOX _uox = null;
	UOX uox { get { if (_uox != null)return _uox; return _uox = new UOX(); } }
	List<CollectionInfoResult> collectionInfo;
	ScriptManager sm;
	public string UsixID { get; set; }
	public int CompanyID { get; set; }
	public int LoanID { get; set; }
	public string OnLoaded { get; set; }
	protected void Page_Load(object sender, EventArgs e) {
	}

	#region UserControl Staff
	protected override void OnInit(EventArgs e) {
		base.OnInit(e);
		fvCollectionInfo.ItemCreated += new EventHandler(fvCollectionInfo_ItemCreated);
		dsCollInfo.Selecting += new EventHandler<LinqDataSourceSelectEventArgs>(dsCollInfo_Selecting);
	}

	protected void dsCollInfo_Selecting(object sender, LinqDataSourceSelectEventArgs e) {
		var ci = new MotoBank().CollectionInfo(LoanID, UsixID, CompanyID, 0);
		if (ci.Count() == 0) {
			var lst = ci.ToList();
			lst.Add(new CollectionInfoResult() { HasDeferment = 1, StatusID_BuyBack = 0, StatusID_Collection = 0 });
			e.Result = lst;
		}else		e.Result = ci;
	}
	protected void dsDeferments_Selecting(object sender, LinqDataSourceSelectEventArgs e) {
		var res = new MotoBank().reports_V_Deferments.Where(s=>s.UsixID == UsixID && s.CompanyID_Raw == CompanyID).ToList();
		if (res.Count() == 0) res.Add(new reports_V_Deferment() { Date= DateTime.MinValue,DeferedDays = 0});
		e.Result = res;
	}

	protected void CollectionInfo_DataBinding(object sender, EventArgs e) {
		HtmlGenericControl gc = sender as HtmlGenericControl;
		uox.applID = Lib.MakeInt(Lib.GetProperty(this, "LoanID"));
		//DataRowView drv = fvCollectionInfo.DataItem as DataRowView;
		//gc.InnerHtml = Data.SQL.XmlInnerText(drv.Row);
	}
	protected void fvCollectionInfo_ItemCreated(object sender, EventArgs e) {
		FormViewEx fv = sender as FormViewEx;
		DataView dv = fv.DataItem as DataView;
	}
	#endregion

	#region Ajax registration
	protected override void OnPreRender(EventArgs e) {
		divCollectionInfo.AppendStyle(this.Attributes);
		if (!this.DesignMode) {
			sm = ScriptManager.GetCurrent(Page);
			if (sm != null) sm.RegisterScriptControl(this);
		}
		this.RegisterCSS("/CSS/CollectionCard.css");
		base.OnPreRender(e);
	}
	protected override void Render(HtmlTextWriter writer) {
		if (!this.DesignMode && sm != null) sm.RegisterScriptDescriptors(this);
		base.Render(writer);
	}
	#endregion

	#region IScriptControl Members

	public IEnumerable<ScriptDescriptor> GetScriptDescriptors() {
		ScriptControlDescriptor descriptor = new ScriptControlDescriptor("motobankcorp.Bridge_CollectionInfo", divCollectionInfo.ClientID);
		descriptor.AddProperty("loanID", LoanID);
		descriptor.AddProperty("companyID", CompanyID);
		descriptor.AddProperty("usixID", UsixID + "");
		if (OnLoaded + "" != "") descriptor.AddEvent("loaded", OnLoaded);
		yield return descriptor;
	}

	public IEnumerable<ScriptReference> GetScriptReferences() {
		yield return new ScriptReference(System.IO.Path.ChangeExtension(AppRelativeVirtualPath, "js"));
	}

	#endregion

	#region IScriptControl Members

	IEnumerable<ScriptDescriptor> IScriptControl.GetScriptDescriptors() {
		return GetScriptDescriptors();
	}

	IEnumerable<ScriptReference> IScriptControl.GetScriptReferences() {
		return GetScriptReferences();
	}

	#endregion
}
